import axios from '../lib/request'
import {ElMessage} from "element-plus"
import {useRouter} from 'vue-router'

const router = useRouter()


export async function login2(url: string, data: any) {
    axios.post(url, data).then(res => {
        let result: any = res.data
        if (result.code === 0) {
            ElMessage.error(result.msg)
        } else {
            //ElMessage.success(result.msg)
            //成功后，保存令牌 cookie
            localStorage.setItem('token', result.token)

            //直接进入home首页
            //router.push({path:'/home'})

            //消息关闭后，进入首页
            let _msg = ElMessage({
                message: `${result.msg}！即将跳转到系统首页！`,//提示的信息
                type: 'success',　　//类型是成功
                offset: 50,　　//距离窗口顶部的偏移量，建议不设置
                duration: 800,　　//显示时间, 毫秒。设为 0 则不会自动关闭，建议1200
                onClose: () => {
                    //跳转页面或执行方法
                    router.push({path: '/home'})
                }
            })
            //3秒后强制关闭
            setTimeout(function () {
                _msg.close();
            }, 3000)
        }
    }).catch(e => {
        ElMessage.error('网络错误，请检查后端接口.')
    })
}


export async function login(url: string, data: any) {
    return await axios.post(url, data)
}

export async function getGoodsList() {
    const {data: res} = await axios.get("/api/goods")
    if (res.status !== 0) return console.log('获取商品列表失败！')
    //goodslist = res.data
}
